﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net


Partial Class mtFundCenterTypeCmmt
    Inherits System.Web.UI.Page

    Public AlertText As String

    Private Shared logger As ILog = LogManager.GetLogger("mtUsersFCenter")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >>  FundCenter Type - Cmmt"

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        'If IsPostBack = False Then
        '    WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
        'End If
        If IsPostBack = False Then

            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
        End If


    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        Dim adapter As New mtFCtypeCmmtTableAdapters.FundCenterTypeCmmtTableAdapter
        Dim dtTable As New mtFCtypeCmmt.FundCenterTypeCmmtDataTable
        If fctrTypeWebCombo.Value <> "" Then
            dtTable = adapter.GetDataByType(fctrTypeWebCombo.Value, Val(Session("PlanYearTxt")))
        Else
            dtTable = adapter.GetDataByPlanYear(Val(Session("PlanYearTxt")))
        End If
        e.DataSource = dtTable
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(5).ForceNoEdit = True
        e.Row.Cells(1).Text = e.Row.DataSourceIndex + 1
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New mtFCtypeCmmtTableAdapters.FundCenterTypeCmmtTableAdapter

        Dim id As Decimal
        Dim exist As Integer
        exist = adapter.CheckExist(e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, e.Row.Cells(4).Value)
        If exist = 0 Then
            If e.Row.Cells(4).Value < e.Row.Cells(3).Value Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Commitment น้อยกว่า จาก กรุณาตรวจสอบ")
            Else
                id = adapter.InsertTypeCmmt(e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), e.Row.Cells(4).Value, Val(Session("PlanYearTxt")), Now(), Session("users"))
            End If
        Else
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูล FundCenter Type - Cmmt ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        End If



            Dim dr As DataRow = e.Row.DataRow
            dr("Id") = id
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New mtFCtypeCmmtTableAdapters.FundCenterTypeCmmtTableAdapter
        Dim exist As Integer
        exist = adapter.CheckExist(e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, e.Row.Cells(4).Value)
        If exist = 0 Then
            If e.Row.Cells(4).Value < e.Row.Cells(3).Value Then
                WebGrid1.ClearCachedDataSource()
                Throw New ArgumentException("ข้อมูลถึง Commitment น้อยกว่า จาก กรุณาตรวจสอบ")
            Else
                adapter.UpdateTypeCmmt(e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), e.Row.Cells(4).Value, Val(Session("PlanYearTxt")), Now(), Session("users"), id)
            End If

        Else
            WebGrid1.ClearCachedDataSource()
            WebGrid1.ClientAction.Refresh()
            Throw New ArgumentException("ข้อมูล FundCenter Type - Cmmt ซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")
        End If

    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New mtFCtypeCmmtTableAdapters.FundCenterTypeCmmtTableAdapter
            If type = "1" Then
                adapter.DeleteQuery(Deletet)
            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteQuery(keyValue)
                Next keyValue
            End If
            WebGrid1.ClientAction.Refresh()
        End If

    End Sub
    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        'If fctrTypeWebCombo.Value = "" Then
        '    Alert("กรุณาเลือก Fund Center Type")
        '    'WebGrid1.LayoutSettings.AllowAddNew = AddNew.No
        '    'Exit Sub
        '    'Else
        '    'WebGrid1.LayoutSettings.AllowAddNew = AddNew.Yes
        'End If
        Session("PlanYearTxt") = PlanYearsTxt.Text
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        't_countdata.Visible = True
        'If Val(Session("count_data")) > 0 Then
        '    L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        'Else
        '    L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        'End If
    End Sub
    Protected Sub FundCenterTypeCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New mtFCtypeUserTableAdapters.FundCenterTypeTableAdapter
        Dim dt As New mtFCtypeUser.FundCenterTypeDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYearTxt")))
        e.DataSource = dt
    End Sub

    Protected Sub fctrTypeWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles fctrTypeWebCombo.InitializeDataSource
        Dim adap As New mtFCtypeUserTableAdapters.FundCenterTypeTableAdapter
        Dim dt As New mtFCtypeUser.FundCenterTypeDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYearTxt")))
        e.DataSource = dt

    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

    Protected Sub RegenBtn_Click(sender As Object, e As ImageClickEventArgs) Handles RegenBtn.Click
        If fctrTypeWebCombo.Value = "" Then
            Alert("กรุณาเลือก Fund Center Type")
            Exit Sub
        End If
        Dim adap As New mtFCtypeCmmtTableAdapters.FundCenterTypeCmmtTableAdapter
        Dim dt As New mtFCtypeCmmt.FundCenterTypeCmmtDataTable
        dt = adap.GetDataByType(fctrTypeWebCombo.Value, Val(Session("PlanYearTxt")))
        Dim adap2 As New mtFCtypeCmmtTableAdapters.FundCenterCmmtsTableAdapter
        adap2.DeleteByType(Val(PlanYearsTxt.Text), fctrTypeWebCombo.Value)
        For Each item As mtFCtypeCmmt.FundCenterTypeCmmtRow In dt
            adap2.GenByFCType(Val(PlanYearsTxt.Text), Session("users"), item.FromCmmt_CmmtCode, item.ToCmmt_CmmtCode, fctrTypeWebCombo.Value, Val(PlanYearsTxt.Text))
        Next
        Alert("ปรับปรุงรายการเรียบร้อย")
    End Sub
End Class
